/*
 * #%L
 * Admin
 * %%
 * Copyright (C) 2011 Ractoc.com
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.ractoc.opengamefinder.admin;

import com.ractoc.opengamefinder.client.ConfigLocator;
import com.ractoc.opengamefinder.client.GuiContainer;
import com.ractoc.opengamefinder.client.plugins.messages.DisplayScreenMessage;
import com.ractoc.opengamefinder.client.plugins.messages.GuiMessage;
import com.ractoc.opengamefinder.client.plugins.nifty.ScreenType;
import com.ractoc.opengamefinder.client.plugins.results.DisplayScreenMessageResult;
import com.ractoc.pffj.api.BasePluginMessageResult;
import com.ractoc.pffj.core.PluginController;
import com.ractoc.pffj.core.PluginException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.log4j.Logger;

/**
 *
 * @author ractoc
 */
public class AdminFactory {

    private static Logger logger = Logger.getLogger(AdminFactory.class.getName());

    private AdminFactory() {
    }

    /**
     * Creates an instance with the supplied GuiContainer. 
     * This is the default startup poinnt and sets up the main login page as the starting page.
     * @param guiContainer The GuiContainer containing the JME3 assets needed to startup Nifty inside JME3.
     * @return The DisplayScreenMessageResult containing the success or failure of the startup.
     * @throws OGFAdminException Something went wrong with the startup of OGF. 
     * This could be either in the startup of PFfJ or the processing of the DisplayScreenMessage.
     */
    public static BasePluginMessageResult createInstance(GuiContainer guiContainer) throws OGFAdminException {
        // Instantiate and startup the plugin controller
        final ExecutorService threadExecutor = Executors.newSingleThreadExecutor();
        String pffjConfigFile = ConfigLocator.getCustomConfig("PFfJAdmin.xml");
        if (pffjConfigFile == null) {
            pffjConfigFile = "/configuration/pffj/PFfJAdmin.xml";
        }
        final Future<Integer> f = threadExecutor.submit((Callable<Integer>) new PluginController(
                pffjConfigFile));
        try {
            f.get();
        }
        catch (final InterruptedException e) {
            logger.error("main plugincontroller thread interrupted.", e);
            throw new OGFAdminException(e);
        }
        catch (final ExecutionException e) {
            logger.error("exception running main plugincontroller thread.", e);
            throw new OGFAdminException(e);
        }
        final DisplayScreenMessage dsMsg = new DisplayScreenMessage();
        dsMsg.setScreenName("loginScreen");
        DisplayScreenMessageResult dsmResult = null;
        final GuiMessage guiMsg = new GuiMessage();
        guiMsg.setPayload(dsMsg);
        guiMsg.setType(ScreenType.LOGIN);
        try {
            dsmResult = (DisplayScreenMessageResult) PluginController.getCurrentPluginController().processMessage(
                    "guiPlugin", guiMsg);
        }
        catch (final PluginException e) {
            throw new OGFAdminException(e);
        }
        return dsmResult;
    }
    
}
